var appControllers = angular.module('appControllers', []);


/************************************************************************/

appControllers.controller('HomeController', ['$scope', '$location', '$http', function ($scope, $location, $http) {
	$scope.Connect = function() {
		var login = $('#login').val();
		var password = $('#password').val();
		password = functions.Crypt(password);
		var url = serv + 'user/' + login + '/' + password;
		var response = datas.Connect($http, url, null);
		response.success(function(data) {
			if(data.Data != null) {
				user = data.Data;
				functions.SetItemInLocalStorage('user', functions.GetJSON(user));
				$location.path('/');
				IsDeviceRegistered(window.device.uuid, window.device.platform);
				window.location.reload();
			}
		}).error(function(data, status, headers, config) {
    		functions.GetError(data);
    	});

		//alert($scope.uuid)


	}

	$scope.Register = function() {
		var login = $('#login').val();
		var password = $('#password').val();
		password = functions.Crypt(password);
		var country = $('#country').val();
		var email = $('#email').val();
		$http.post(serv + 'user/add/' + login + '/' + email + '/' + country + '/' + password).success(function(data) {
			if(data.Data != null) {
				user = data.Data;
				functions.SetItemInLocalStorage('user', functions.GetJSON(user));
			}
		}).Error(function(data) {
			functions.GetError(data);
		});

		//IsDeviceRegistered(window.device.uuid, window.device.platform);
	}

	function IsDeviceRegistered(uuid, platform) {
		var req = functions.GetRequestParams(user.Id, functions.GetSignature(user, salt), null);
		$http.post(serv + 'device/add/' + user.Id + '/' + uuid + '/' + user.Id + '/' + platform, { request: req }).success(function(data) {
			alert(data.Data);
			functions.GetError(data);
		});
	}

	if(user != null) {
		$scope.User = user;
	}
}]);


/************************************************************************/

appControllers.controller('CameraController', ['$scope', '$location', '$http', function ($scope, $location, $http) {
	
	function GetPicture() {
		navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
        destinationType: destinationType.DATA_URL });
	};

	var destinationType = navigator.camera.DestinationType;
	GetPicture();

	function onPhotoDataSuccess(imageData) {
    	var smallImage = document.getElementById('smallImage');
    	smallImage.src = "data:image/jpeg;base64," + imageData;
    	$scope.img = imageData;
    }

    function onFail(message) {
    	alert('Failed because: ' + message);
    }

    $scope.SendPicture = function() {
    	AddPicture();
    }

    function AddPicture() {
    	var place = $('#place').val();
    	var imgSrc = $scope.img;
    	var req = functions.GetRequestParams(user.Id, functions.GetSignature(user, salt), imgSrc);
    	var response = datas.AddPicture($http, serv + 'picture/add/' + user.Id + '/' + place + '/' + '2;7', req);
    	response.success(function(data) {
    		if(data.Error != null) {
    			functions.GetError(data.Error);
    		} else {
    			$location.path('/camera');
    		}
    	}).error(function(data, status, headers, config) {
    		functions.GetError(data);
    	});
    }

}]);



/************************************************************************/

appControllers.controller('CircleController', ['$scope', '$location', '$http', '$routeParams', function ($scope, $location, $http, $routeParams) {
	
	var path = $location.path();
	if(path == '/circle') {
		GetCircles();
	}

	function GetCircles() {
		var req = functions.GetRequestParams(user.Id, functions.GetSignature(user, salt), null);
		var response = datas.GetCircles($http, serv + 'circle/friends/' + user.Id, req);
		response.success(function(data){
			if(data.Error != null) {
				functions.GetError(data.Error);
			} else {
				$scope.Circles = data.Data;
			}
		}).error(function(data){
			functions.GetError(data);
			$scope.Error = data;
		});
	}

	$scope.getUser = function(id) {
		$location.path('/user/' + id);
	}

}]);


/************************** UserController ***********************************************/


appControllers.controller('UserController', ['$scope', '$location', '$http', '$routeParams', function ($scope, $location, $http, $routeParams) {
	
	var path = $location.path();

	switch(path) {
		case '/user/' + $routeParams.id:
			GetUserById($routeParams.id);
			break;
		case '/followed':
			GetUsersFollowed();
			break;
		case '/follower':
			GetUsersFollower();
			break;
		default:
			break;
	}

	$scope.getUser = function(id) {
		$location.path('/user/' + id);
	}

	function GetUserById(id) {
		var req = functions.GetRequestParams(user.Id, functions.GetSignature(user, salt), null);
		datas.GetUserById($http, serv + 'user/' + id, req)
		.success(function(data){
			if(data.Error != null) {
				functions.GetError(data.Error);
			} else {
				$scope.user = data.Data;
			}
		}).error(function(data){
			functions.GetError(data);
			$scope.Error = data;
		});
	}

	function GetUsersFollowed() {
		var req = functions.GetRequestParams(user.Id, functions.GetSignature(user, salt), null);
		datas.GetUsersFollowed($http, serv + 'users/followed/' + user.Id, req)
		.success(function(data){
			if(data.Error != null) {
				functions.GetError(data.Error);
			} else {
				$scope.users = data.Data;
				$scope.type = 'abonnements';
			}
		}).error(function(data){
			functions.GetError(data);
			$scope.Error = data;
		});
	}

	function GetUsersFollower() {
		var req = functions.GetRequestParams(user.Id, functions.GetSignature(user, salt), null);
		datas.GetUsersFollowed($http, serv + 'users/follower/' + user.Id, req)
		.success(function(data){
			if(data.Error != null) {
				functions.GetError(data.Error);
			} else {
				$scope.users = data.Data;
				$scope.type = 'abonnés';
			}
		}).error(function(data){
			functions.GetError(data);
			$scope.Error = data;
		});
	}

	/*function GetUserById(id) {
		var req = functions.GetRequestParams(user.Id, functions.GetSignature(user, salt));
		$http.post(serv + 'user/' + id, { request: req })
		.success(function(data){
			if(data.Error != null) {
				functions.GetError(data.Error);
			} else {
				$scope.User = data.Data;
			}
		}).error(function(data){
			functions.GetError(data);
			$scope.Error = data;
		});
	}*/

}]);
